import { queryList, queryDetail } from '../services';
import { queryProcess } from '../../services';
import { formDataFormat } from '../../utils';

export default {
    namespace: 'flowMyAudited',
    state: {
        myAuditedList: [],
        pagination: {
            current: 1,
            pageSize: 10,
            total: 0
        },
        detailData: {},
        processList: []
    },

    effects: {
        *fetch({ payload, callback }, { call, put }) {
            const response = yield call(queryList, payload);
            yield put({
                type: 'save',
                payload: {
                    // order: payload.orderBy,
                    myAuditedList: response.data.rows,
                    pagination: {
                        pageSize: response.data.pageSize,
                        current: response.data.pageNo,
                        total: response.data.total
                    }
                }
            });
            if (callback) {
                callback(response);
            }
        },
        *fetchDetail({ payload, callback }, { call, put }) {
            const response = yield call(queryDetail, payload);
            if (response && response.code === 10000) {
                const returnData = response.data;
                // 把返回的表单值和表单做处理
                const newData = formDataFormat(returnData);

                yield put({
                    type: 'save',
                    payload: {
                        detailData: { ...returnData, formList: newData }
                    }
                })
                if (callback) {
                    callback(response);
                }
            }
        },
        *fetchProcess({ payload, callback }, { call, put }) {
            const response = yield call(queryProcess, payload);
            if (response.code === 10000) {
                yield put({
                    type: 'save',
                    payload: {
                        processList: response.data
                    }
                });
            } else {
                yield put({
                    type: 'save',
                    payload: {
                        processList: []
                    }
                });
            }
            if (callback) {
                callback(response);
            }
        }
    },
    reducers: {
        save(state, { payload }) {
            console.log('payload', payload);
            return {
                ...state,
                ...payload
            };
        }
    }
};
